<cfcomponent output="false">
	
	<cffunction name="facebookLogin" access="public" output="true" returntype="void">
		<cfargument name="reauthenticate" default="true" required='false' />

		<cfset var local = {} />
		<cfset session.state = Hash(createuuid(),'md5')>
		<cfset local.url = "https://www.facebook.com/dialog/oauth?">
		<cfset local.client_id = "client_id=" & $.site.getVar("app_id") />
		<cfset local.redirect_uri = "&redirect_uri=" & urlEncode($.site.getVar("my_url")) />
		<cfset local.state = "&state=" & session.state />
		<cfset local.scope = "&scope=email,user_birthday" />
		<cfif arguments.reauthenticate>
			<cfset local.auth = "&auth_type=reauthenticate" />
		<cfelse>
			<cfset local.auth = "" />
		</cfif>
			
		<cflocation url="#local.url##local.client_id##local.redirect_uri##local.state##local.scope##local.auth#" addToken="no" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="handleFacebookLogin" access="public" output="true" returntype="void">

		<cfset var local = {} />

		<!--- handle good response --->
		<cfif structKeyExists(arguments,"state") and structKeyExists(session,"state") and structKeyExists(arguments,'code') and session.state eq arguments.state>
			<cftry>
				
				<cfset local.thisURL = "https://graph.facebook.com/oauth/access_token?client_id=#$.site.getVar("app_id")#&redirect_uri=#urlEncode(application.q.getVar("my_url"))#&client_secret=#application.q.getVar("app_secret")#&code=#arguments.code#">
				
				<cfhttp url="#local.thisURL#" throwonerror="true">
				<cfset local.response = cfhttp.filecontent />
				<cfset session.facebookAccessToken = listLast(listFirst(local.response,"&"),"=") />
				<!--- <cfset application.s.loginService.facebookLoginSuccess() /> --->
				<cfset $.run.it('loginService.facebookLoginSuccess', {}) />
				<cfcatch>
					<cfset $.session.addMessage("Error:loginController.handleFacebookLogin #cfcatch.message#") />
					<cflocation url="#$.site.getVar("homeURL")#" addToken="no" />
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset facebookLogin(false) />
		</cfif>
		
		<cflocation url="#$.site.getVar("homeURL")#?facebookLogin" addToken="no" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="logout" access="public" output="true" returntype="void">

		<cfset var local = {} />
		
		<!--- <cfset application.s.loginService.logout() /> --->
		<cfset $.run.it('loginService.logout', {}) />
		
		<cflocation url="#$.site.getVar('homeURL')#" addToken="no" />

	</cffunction>
	
	<!--- --->

</cfcomponent>